package com.showdoc.controller;

import com.showdoc.mapper.TeamMapper;
import com.showdoc.mapper.TeamMemberMapper;
import com.showdoc.mapper.UserMapper;
import com.showdoc.pojo.Team;
import com.showdoc.pojo.TeamMember;
import com.showdoc.pojo.User;
import com.showdoc.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

//团队成员的控制层
@CrossOrigin
@RestController
@RequestMapping("teamMember")
@EnableAutoConfiguration
public class TeamMemberController {
    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TeamMemberMapper teMemMapper;

    @Autowired
    private UserMapper userMapper;

    //查询团队成员
    @GetMapping("/getList")
    public R getList (@Param("teamId") Long teamId) {
        //团队成员加上leader
        //根据teamId 查询team
        Team team = teamMapper.selectById(teamId);
        long uid = team.getUid();
        User leader = userMapper.selectByUid(uid);

        //创建一个临时的成员对象并封装
        TeamMember temp = new TeamMember();
        temp.setMemberUserName(leader.getUsername());
        temp.setAddTime(leader.getReg_time());
        temp.setTeamId(teamId);
        temp.setMemberUid(leader.getUid());
        temp.setLastUpdateTime(leader.getLast_login_time());
        temp.setTeamMemberGroupId(2);

        List<TeamMember> teamMembers = new ArrayList<>();
        List<TeamMember> members = teMemMapper.selectByTeamId(teamId);
        teamMembers.add(temp);

        for(TeamMember t : members){
            teamMembers.add(t);
        }

        return R.success(teamMembers);
    }

    //添加团队成员
    @PostMapping("/save")
    public R<TeamMember> addMember(
//                       @Param("teamId")Long teamId,
//                       @Param("memberUserName") String memUserName,
//                       @Param("team_member_group_id") int id
                    @RequestBody TeamMember temp
                       )
    {
        //查询用户是否存在
        String memberUserName = temp.getMemberUserName();
        User user = userMapper.findByName(memberUserName);
        if(user == null){
            return R.error("您要添加的用户不存在");
        }else {
//          TeamMember temp = new TeamMember();
//          temp.setTeamId(teamId);
//          temp.setMemberUserName(memUserName);
//          temp.setTeam_member_group_id(id);
          temp.setMemberUid(user.getUid());
          Timestamp time = new Timestamp(System.currentTimeMillis());
          temp.setAddTime(time);
          temp.setLastUpdateTime(time);
          teMemMapper.save(temp);
          return R.success(temp);
        }
    }

    //删除团队成员的Controller
    @PostMapping("/delete")
    public R memberDelete(@RequestBody TeamMember t){
        //在这里先假设要删除的是leader
        try {
            teMemMapper.delete(t);
            if(t.getTeamMemberGroupId() == 2)
            {
                return R.error("不能对团队管理员执行此操作");
            }
            return R.success("删除成功");
        }catch(NullPointerException e) {
            return R.error("不能对团队领导人执行此操作");
        }
    }
}
